package optimizers.ganeat.report;

import util.MyUtil;

import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import com.itextpdf.awt.PdfGraphics2D;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

import optimizers.commons.GAConstants;
import optimizers.commons.RunConfig;
import optimizers.ganeat.Connection;
import optimizers.ganeat.Constants;
import optimizers.ganeat.Individual;
import optimizers.ganeat.Species;
import visualization.GraphDisplayer;
import visualization.PlotExpData;
import visualization.PlotIndividualHistory;
import visualization.ganeat.PlotSpeciesData;

public class ReaderNEAT {

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 */
	public static void openReport(File file) throws ClassNotFoundException {
		FileInputStream saveFile;
		try {
			saveFile = new FileInputStream(file);
			ObjectInputStream restore = new ObjectInputStream(saveFile);
			ArrayList<ArrayList<Species>> speciesVisualData = (ArrayList<ArrayList<Species>>) restore
					.readObject();
			try {

				JFrame graphFrame1 = new JFrame("Species Visualization");
				graphFrame1.setSize(1500, 800);
				GridLayout grid1 = new GridLayout(0, 1);
				JPanel panel1 = new JPanel();
				panel1.setLayout(grid1);
				grid1.setColumns(1);
				graphFrame1.add(panel1);
				PlotSpeciesData demo4 = new PlotSpeciesData(speciesVisualData,
						1500, 800);
				panel1.add(demo4);
				graphFrame1.setVisible(true);
				double[][] speciesData = (double[][]) restore.readObject();
				String consoleText = (String) restore.readObject();
				Map<String, Integer> innovations = (Map<String, Integer>) restore
						.readObject();
				Map<String, String> nodeNames = (Map<String, String>) restore
						.readObject();
				ArrayList<Individual> speciesLib = (ArrayList<Individual>) restore
						.readObject();
				RunConfig config = (RunConfig) restore.readObject();
				PlotExpData demo = MyUtil.getSpeciesFitnessPlot(
						speciesVisualData, demo4.getIndivColor());
				PlotExpData demo3 = new PlotExpData(
						"Number of species over generations", speciesData,
						new String[] { "nSpecies", "Speciation Threshold" },
						"Generation", "Number of species");
				JFrame graphFrame = new JFrame("Metrics over generations");
				graphFrame.setSize(500, 600);
				GridLayout grid = new GridLayout(0, 1);
				JPanel panel = new JPanel();
				panel.setLayout(grid);
				grid.setColumns(1);
				graphFrame.add(panel);
				panel.add(demo);
				panel.add(demo3);
				graphFrame.setVisible(true);
				GraphDisplayer bestIndividuals = new GraphDisplayer(1024, 768);
				JFrame frame = new JFrame();
				frame.add(bestIndividuals);
				frame.setSize(1024, 768);
				frame.setVisible(true);
				for (int i = speciesVisualData.size() - 1; i < speciesVisualData
						.size(); i++) {
					Individual bestIndividual = null;
					for (Species sp : speciesVisualData.get(i)) {
						if (bestIndividual == null
								|| bestIndividual.getFitness() < sp
										.getBestIndividual().getFitness()) {
							bestIndividual = sp.getBestIndividual();
							bestIndividual.config = config;
						}
						for (Individual indiv : sp.individuals) {
							if (indiv.individualIndex == 28) {
								bestIndividuals.addGraph(indiv.getOligoModel()
										.getVisualization(250, 250), indiv
										.displayTimeSeries(), indiv
										.printModelInTextArea());
								bestIndividuals.display();
							}
						}
					}
					bestIndividuals.addGraph(bestIndividual.getOligoModel()
							.getVisualization(250, 250), bestIndividual
							.displayTimeSeries(), bestIndividual
							.printModelInTextArea());
					bestIndividuals.display();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			restore.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws ClassNotFoundException,
			IOException {
		// TODO Auto-generated method stub

		JFileChooser fc = new JFileChooser();
		int returnVal;
		/*
		 * do { returnVal = fc.showOpenDialog(null); } while (returnVal !=
		 * JFileChooser.APPROVE_OPTION && returnVal !=
		 * JFileChooser.CANCEL_OPTION); File file = fc.getSelectedFile();
		 */
		File file = new File(
				"C:\\Users\\Huy\\workspace_new\\SB_Project\\result\\pref_Math_2014_03_30 23_08_02\\log");
		System.out.println(file.toString());
		/*
		 * FileInputStream saveFile = new FileInputStream(file);
		 * ObjectInputStream restore = new ObjectInputStream(saveFile);
		 * ArrayList<ArrayList<Species>> speciesVisualData =
		 * (ArrayList<ArrayList<Species>>) restore .readObject(); JPanel panel =
		 * new JPanel(); JFrame frame = new JFrame(""); frame.add(panel);
		 * frame.setSize(1200, 800); Individual bestIndividual = null; for
		 * (Species sp : speciesVisualData.get(speciesVisualData.size() - 1)) {
		 * if (bestIndividual == null || bestIndividual.getFitness() <
		 * sp.getBestIndividual() .getFitness()) { bestIndividual =
		 * sp.getBestIndividual(); } } PlotIndividualHistory demo = new
		 * PlotIndividualHistory("", bestIndividual); panel.add(demo);
		 * frame.setVisible(true);
		 */
		openReport(file);
	}
}
